package week10;

import java.util.TreeMap;
import java.util.TreeSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class Pta74 {

	public static void main(String[] args) {
		TreeMap<String ,ArrayList<Integer>> dict = new TreeMap<String ,ArrayList<Integer>>();
		Scanner sc = new Scanner(System.in 

);
		int count = 1 ;
		ArrayList<String> lineList = new ArrayList<String> ();
		while(sc.hasNextLine()){
			String  line = sc.nextLine();
			if("!!!!!".equals(line)) break;
			lineList.add(line);
			String[] words = line.split(" +");
			for (int i = 0; i < words.length; i++) {
				if(dict.containsKey(words[i])) {
					dict.get(words[i]).add(count);
					dict.put(words[i], dict.get(words[i]));
				}
				else {
					ArrayList<Integer> list = new ArrayList<Integer> ();
					list.add(count);
					dict.put(words[i], list);
				}
			}
			count++;
		}
		System.out.println(dict);
		while(sc.hasNextLine()) {
			String  line = sc.nextLine();
			String[] words = line.split(" +");
			TreeSet<Integer> co = new TreeSet<Integer> ();
			for (int i = 0; i < words.length; i++) {
				if(dict.containsKey(words[i])) 
					for (int j = 0; j < dict.get(words[i]).size(); j++) {
						co.add(dict.get(words[i]).get(j));
					}
				else {
					co.clear();
					break;
				}
			}
			if(co.size() == 0) {
				System.out.println("found 0 results");
			}
			else {
				Iterator<Integer > it = co.iterator();
				boolean f = false;
				System.out.print("[");
				for (Integer integer : co) {
					if(f) System.out.print(", " + integer);
					else {
						System.out.print(integer);
						f = true;
					}
				}
				System.out.println("]");
				while(it.hasNext()) {
					System.out.println(lineList.get(it.next().intValue()-1));
				}
			}
		}
		sc.close();
	}

}
